package com.zhu.mywxpay.controller;

import com.wechat.pay.contrib.apache.httpclient.exception.HttpCodeException;
import com.wechat.pay.contrib.apache.httpclient.exception.NotFoundException;
import com.zhu.mywxpay.Vo.WxPayRespVO;
import com.zhu.mywxpay.service.WxPayService;
import com.zhu.mywxpay.util.RandomStringGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.net.URISyntaxException;
import java.security.*;
import java.util.Map;


@Slf4j
@RestController
@RequestMapping("/wx")
public class wxPayController {
    @Resource
    private WxPayService wxPayService;
    @PostMapping("/payNotify")
    private Map payNotify(HttpServletRequest request)  {
        return wxPayService.payNotify(request);
    }
    @PostMapping("/payRefund")
    private Map payRefund(HttpServletRequest request)  {
        return wxPayService.payRefund(request);
    }

    @GetMapping("/order")
    private WxPayRespVO  OrderPay(String code,String amount) throws GeneralSecurityException, NotFoundException, IOException, HttpCodeException, URISyntaxException {
        String description="商品描述小朱";
        String Out_trade_no= RandomStringGenerator.getRandomStringByLength(32);
        log.info("发起支付请求，商品描述：{}", description);
        log.info("发起支付请求，商品订单号：{}", Out_trade_no);
        WxPayRespVO wxPayRespVO = wxPayService.nativePay(description, amount,code, Out_trade_no);
        log.info("返回的实体参数： "+wxPayRespVO);
        return  wxPayRespVO;
    }
    @GetMapping("/refund")
    private void refund(String Out_trade_no) throws IOException, GeneralSecurityException, NotFoundException, HttpCodeException {
        String out_refund_no= RandomStringGenerator.getRandomStringByLength(32);
        String reason="小朱退款";
        String amount="1";
        log.info("发起退款请求,退款id：{}", out_refund_no	);
        wxPayService.refund(Out_trade_no,out_refund_no,reason,amount);
    }


}
